/**
 * 
 */
package com.et114.components.image;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.et114.core.common.AppContext;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

/*
 * @author guanhw
 */
public class ImageResizerV3 {
	protected static Log logger = LogFactory.getLog( ImageResizerV3.class );
	public static byte IMAGE_TYPE_GIF = 0x1 ;
	public static String IMAGE_TYPE_GIF_KEY = "GIF" ; 
	public static byte IMAGE_TYPE_JPG = 0x2 ;
	public static String IMAGE_TYPE_JPG_KEY = "JPG" ; 
	public static byte IMAGE_TYPE_PNG = 0x3 ;
	public static String IMAGE_TYPE_PNG_KEY = "PNG" ; 
	
	/**
	 * 
	 */
	public ImageResizerV3 ( ) {
		// TODO Auto-generated constructor stub
	}
	

	
	/**
	 * 图像缩放 jpg格式
	 * 
	 * @param imgsrc  :原图片文件路径
	 * @param imgdist :生成的缩略图片文件路径
	 * @param widthdist :生成图片的宽度
	 * @param heightdist :生成图片的高度
	 */
	public static void reduceImgRealPath ( String realSrc , String realDePath , String desfile ,
			int widthdist , int heightdist , String type ) throws Exception {
		realSrc = AppContext.realPath + realSrc;

		File file = new File( realDePath ) ; 
		if ( !file.exists ( ) ) file.mkdirs ( ) ; 
			
		desfile = realDePath + desfile ; 
		file = new File( desfile ) ; 
		if ( !file.exists ( )) file.createNewFile ( ) ; 
			
		
		logger.info ( "realSrc: " + realSrc ) ; 
		logger.info ( "realDes: " + desfile ); 
		/*
		 * if ( IMAGE_TYPE_GIF_KEY .equals ( type ) ) reduceImg ( realSrc ,
		 * desfile , widthdist , heightdist , IMAGE_TYPE_GIF_KEY ); if (
		 * IMAGE_TYPE_PNG_KEY .equals ( type ) ) reduceImg ( realSrc , desfile ,
		 * widthdist , heightdist , IMAGE_TYPE_PNG_KEY ); if (
		 * IMAGE_TYPE_JPG_KEY .equals ( type ) )
		 */
			reduceImgJpg ( realSrc , desfile , widthdist , heightdist );
	}
	
	public static void reduceImgJpg ( String imgsrc , String imgdist ,
			int widthdist , int heightdist ) throws Exception   {
		FileOutputStream out = null ; 
		Graphics2D g  = null ; 
		try {
			File srcfile = new File ( imgsrc );
			if ( ! srcfile.exists ( ) ) {
				return;
			}
			Image src = ImageIO.read ( srcfile );
			
			BufferedImage tag = new BufferedImage ( ( int ) widthdist ,
					( int ) heightdist , BufferedImage.TYPE_INT_RGB );
			/*
			 * Image.SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的 优先级比速度高 生成的图片质量比较好 但速度慢
			 */
			g = tag.createGraphics ( );
			//g.setComposite ( AlphaComposite.Src );
			g.setBackground ( Color.white );
			g.clearRect ( 0 , 0 , widthdist , heightdist );
			g.drawImage ( src.getScaledInstance ( widthdist , heightdist ,
					Image.SCALE_SMOOTH ) , 0 , 0 , null );
			g.dispose ( ) ;
			
			out = new FileOutputStream ( imgdist );
			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder ( out );
			encoder.encode ( tag );
		} catch ( IOException ex ) {
			ex.printStackTrace ( );
		} finally {
			try {
				if ( out != null )
					out.close ( );
				if ( g != null )
					g.dispose ( );
			} catch ( Exception ex ) {
				ex.printStackTrace ( );
			}
			
		}
	}

	public static void reduceImg ( String imgsrc , String imgdist ,
			int widthdist , int heightdist , String format )  throws Exception   {
		Graphics2D g  = null ; 		
		try {
			File srcfile = new File ( imgsrc );
			if ( ! srcfile.exists ( ) ) {
				return;
			}
			Image src = ImageIO.read ( srcfile );
			
			BufferedImage tag = new BufferedImage ( ( int ) widthdist ,
					( int ) heightdist , BufferedImage.TYPE_INT_RGB );
			/*
			 * Image.SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的 优先级比速度高 生成的图片质量比较好 但速度慢
			 */
			g = tag.createGraphics ( );
			g.setBackground ( Color.white );
			g.clearRect ( 0 , 0 , widthdist , heightdist );
			g.drawImage ( src.getScaledInstance ( widthdist , heightdist ,
					Image.SCALE_SMOOTH ) , 0 , 0 , null );
			g.dispose ( ) ;
			
			ImageIO.write ( tag , format , new File ( imgdist ) );
			
		} catch ( IOException ex ) {
			ex.printStackTrace ( );
		}
	}
	
	/**
	 * 根据图片路径 读取图片文件
	 * 
	 * @param fileName
	 * @return
	 */
	public static BufferedImage readImage ( String fileName )  throws Exception   {
		BufferedImage bi = null;
		try {
			bi = ImageIO.read ( new File ( fileName ) );
		} catch ( IOException ioe ) {
			ioe.printStackTrace ( );
		}
		return bi;
	}
	
	/**
	 * 生成新的图片文件
	 * 
	 * @param im
	 * @param formatName
	 * @param fileName
	 * @return
	 */
	public static boolean writeImage ( RenderedImage im , String formatName ,
			String fileName )  throws Exception   {
		boolean result = false;
		try {
			result = ImageIO.write ( im , formatName , new File ( fileName ) );
		} catch ( IOException ioe ) {
			ioe.printStackTrace ( );
		}
		return result;
	}
	
	/**
	 * 转换图片格式 到 jpg
	 * 
	 * @param im
	 * @param fileName
	 * @return
	 */
	public static boolean writeJPEGImage ( RenderedImage im , String fileName )  throws Exception   {
		return writeImage ( im , "JPEG" , fileName );
	}
	
	/**
	 * 转换图片格式 到 gif 不知到好用不
	 * @param im
	 * @param fileName
	 * @return
	 */
	public static boolean writeGIFImage ( RenderedImage im , String fileName )  throws Exception  {
		return writeImage ( im , "GIF" , fileName );
	}
	
	public static boolean writePNGImage ( RenderedImage im , String fileName )  throws Exception  {
		return writeImage ( im , "PNG" , fileName );
	}
	
	public static boolean writeBMPImage ( RenderedImage im , String fileName )  throws Exception   {
		return writeImage ( im , "BMP" , fileName );
	}
	
	BufferedImage createResizedCopy ( Image originalImage , int scaledWidth ,
			int scaledHeight ) {
		BufferedImage scaledBI = new BufferedImage ( scaledWidth ,
				scaledHeight , BufferedImage.TYPE_INT_RGB );
		Graphics2D g = scaledBI.createGraphics ( );
		g.setComposite ( AlphaComposite.Src );
		g.drawImage ( originalImage , 0 , 0 , scaledWidth ,
						scaledHeight , null );
		g.dispose ( );
		return scaledBI;
	}
	
	public static void main( String[] ss ) {
		for ( int i = 0 ; i < 1 ; i ++  ) {
			try {
				ImageResizerV3
				.reduceImg(
						"D:/bak/tmp/94d96484-40f3-4ab3-af5d-767b38808648.png" ,
						"D:/bak/tmp/94d96484-40f3-4ab3-af5d-767b38808648-small.png" ,
						100 , 100 , IMAGE_TYPE_PNG_KEY );				
			}
			catch ( Exception ex ) {
				ex.printStackTrace ( );
			}
		}
	}
}
